package com.yztc.ui.home.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yztc.bean.home.BasicBean;
import com.yztc.bean.home.ListBean;
import com.yztc.chunboapp.R;
import com.yztc.utils.ImageHelper;

import java.util.List;

/**
 * Created by My on 2016/10/7.
 */
public class HomeGVBaseAdapter extends BaseAdapter{
    public static final int TYPE_COUNT = 2;
    public static final int TYPE_ONE = 0;
    public static final int TYPE_TWO = 1;

    private List<ListBean> mListBean;
    private BasicBean mBasic;
    private Context mContext;
    public HomeGVBaseAdapter(List<ListBean> mListBean,Context mContext,BasicBean mBasic){
        this.mContext = mContext;
        this.mListBean = mListBean;
        this.mBasic = mBasic;
    }
    @Override
    public int getCount() {
        return mListBean.size();
    }


    @Override
    public Object getItem(int position) {
        return mListBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder01 holder01 = null;
          ViewHolder02 holder02 = null;

      if (position < mListBean.size()-1) {
          if (convertView == null) {

              convertView = LayoutInflater.from(mContext).inflate
                      (R.layout.home_recycleview_gv_item01, null);
              holder01 = new ViewHolder01();
              holder01.ivUrl = (ImageView) convertView.findViewById(R.id.iv_url);
              holder01.ivQiangGou = (ImageView) convertView.findViewById(R.id.iv_qiangGou);
              holder01.tvShortname = (TextView) convertView.findViewById(R.id.tv_shortname);
              holder01.tvSubname = (TextView) convertView.findViewById(R.id.tv_subname);
              holder01.tvSpecifications = (TextView) convertView.findViewById(R.id.tv_specifications);
              holder01.tvChunboPrice = (TextView) convertView.findViewById(R.id.tv_chunbo_price);
              holder01.tvDiscountPrice = (TextView) convertView.findViewById(R.id.tv_discount_price);
              convertView.setTag(holder01);
          } else {
              holder01 = (ViewHolder01) convertView.getTag();
          }
          ImageHelper.getNormalImage(mContext, mListBean.get(position).getUrl(), holder01.ivUrl);
          holder01.tvShortname.setText(mListBean.get(position).getShortname());
          holder01.tvSubname.setText(mListBean.get(position).getSubname());
          holder01.tvSpecifications.setText(mListBean.get(position).getSpecifications());
          String chunboPrice = mListBean.get(position).getChunboPrice();
          String discountPrice = mListBean.get(position).getDiscountPrice();

          //判断价格是否打折
          if ("0.00".equals(discountPrice)) {
              holder01.tvChunboPrice.setText("¥" + chunboPrice);
          } else {
              holder01.tvChunboPrice.setText("¥"+chunboPrice);
              holder01.tvChunboPrice.getPaint()
                      .setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
              holder01.tvDiscountPrice.setText("¥"+discountPrice);
              holder01.ivQiangGou.setImageResource(R.mipmap.xsqg);
          }
       //最后一个item
      }else if (position == mListBean.size()-1){
            if (convertView == null){
                convertView = LayoutInflater.from(mContext).inflate(R.layout.home_recycleview_gv_item02,null);
                holder02 = new ViewHolder02();
                holder02.ivUrl = (ImageView) convertView.findViewById(R.id.iv_m_title_pic);
                convertView.setTag(holder02);
            }else {
                holder02 = (ViewHolder02) convertView.getTag();
            }
                 ImageHelper.getNormalImage(mContext,mBasic.getMTitlePic(),holder02.ivUrl);
      }
        return convertView;
    }

    //前几个布局格式
    static class ViewHolder01{
        ImageView ivUrl,ivQiangGou;
        TextView tvShortname,tvSubname,tvSpecifications,tvDiscountPrice,
                tvChunboPrice;
    }
    //最后一个item的布局
    static class ViewHolder02{
        ImageView ivUrl;
    }
}
